package com.android.model.login;

import android.util.Log;

import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.utils.constraint.Constants;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModelImpl implements LoginModel{
    private final static String TAG = "LoginModelImpl";

    /**
     * 用户登录功能
     * @param account 手机号
     * @param phoneCode 验证码
     * @param loginListener 反回数据给活动
     */
    @Override
    public void longin(String account,String phoneCode,final LoginListener loginListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        longinHttp_Interface request  = retrofit.create(longinHttp_Interface.class);

        User user = new User();
        user.setCode(phoneCode);
        user.setPhone(account);

        Observable<ResponseData<User>> observable = request.login(user);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<User> userResponseData) {
                        loginListener.onLoginSuccess(userResponseData);
                        Log.e("login",userResponseData.getMsg());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("onError",e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取验证码
     * @param phoneNumber 手机号
     */
    @Override
    public void sendVerifyCode(String phoneNumber,final LoginListener loginListener) {
        Retrofit retrofit = new  Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        longinHttp_Interface httpInterface = retrofit.create(longinHttp_Interface.class);

        Observable<ResponseData<String>> observable = httpInterface.sendCode(phoneNumber);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<String>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<String> stringResponseData) {
                        loginListener.onGetCodeSuccess(stringResponseData);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG,"error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    /**
     * 用户注册
     * @param phoneNumber
     * @param code
     */
    @Override
    public void registerUser(String phoneNumber, String code,final LoginListener loginListener) {
        Retrofit retrofit = new  Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        longinHttp_Interface httpInterface = retrofit.create(longinHttp_Interface.class);

        User user = new User();
        user.setPhone(phoneNumber);
        user.setCode(code);
        Observable<ResponseData<String>> observable = httpInterface.register(user);
        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<String>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<String> stringResponseData) {
                        loginListener.onRejisterSuccess(stringResponseData);
                        Log.e(TAG,"注册成功");

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
